//
//  SheetView.m
//  答案的表视图控件
//
//  Created by dcr on 2017/1/18.
//  Copyright © 2017年 邓兆太. All rights reserved.
//

#import "SheetView.h"
@interface SheetView()
{
    UIView *_superView;
    UIScrollView *_scrollView;
    BOOL _startMoving;
    float _height;
    float _width;
    float _y;
    int _count;
}
@end

@implementation SheetView

- (instancetype)initWithFrame:(CGRect)frame withSuperView:(UIView *)superView andQuesCount:(int)count{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _superView = superView;
        _height = frame.size.height;
        _width = frame.size.width;
        _y = frame.origin.y;
        _count = count;
        [self createView];
    }
    return self;
}

- (void)createView{
    _backView = [[UIView alloc] initWithFrame:_superView.frame];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0;
    [_superView addSubview:_backView];
    
    //滚动区域大小
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 70, self.frame.size.width, self.frame.size.height-70)];
    
    [self addSubview:_scrollView];
    //根据题数添加按钮
    for(int i = 0; i < _count; i++){
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        //每行按钮数不超过6个
        btn.frame = CGRectMake((_width-44*6)/2+44*(i%6), 10+44*(i/6), 40, 40);
        btn.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1];
        if(i == 0){
            btn.backgroundColor = [UIColor orangeColor];
        }
        btn.tag = 100+i;
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        //按钮的标题，题目的索引值
        [btn setTitle:[NSString stringWithFormat:@"%d", i+1] forState:UIControlStateNormal];
        [_scrollView addSubview:btn];
    }
    //如果不能整除需要再加一行
    int tip = (_count%6)?1:0;
    _scrollView.contentSize = CGSizeMake(0, 20+44*(_count/6+1+tip));
}

//点击按钮的响应事件，使用代理把索引传出
-(void)click:(UIButton*) sender{
    int index = sender.tag-100;
    for(int i = 0; i < _count; i++){
        UIButton *btn = [self viewWithTag:i+100];
        if(i == index){
            btn.backgroundColor = [UIColor orangeColor];
        }else{
            btn.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1];
        }
    }
    [_delegate SheetViewClick:index];
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:[touch view]];
    if(point.y < 25){
        _startMoving = true;
    }
    if(_startMoving && self.frame.origin.y >= _y-_height && [self convertPoint:point toView:_superView].y >= 80){
        //根据滑动改变背景的透明值
        self.frame = CGRectMake(0, [self convertPoint:point toView:_superView].y, _width, _height);
        _backView.alpha = (_superView.frame.size.height-self.frame.origin.y)/_superView.frame.size.height*0.8;
    }
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    _startMoving = false;
    //滑动结束，判断位置是否显示弹出框
    if(self.frame.origin.y > _y - _height/2){
        [UIView animateWithDuration:0.3 animations:^{
            self.frame = CGRectMake(0, _y, _width, _height);
            _backView.alpha = 0;
        }];
    }else{
        [UIView animateWithDuration:0.3 animations:^{
            self.frame = CGRectMake(0, _y-_height, _width, _height);
            _backView.alpha = 0.8;
        }];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
